<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function breadcumb_html(){
	if(!isset($_SESSION['sys_breadcumb'])){
		$_SESSION['sys_breadcumb'][1] = array( language_translate('breadcumb_home') , site_url_lang('home') );
	}
	ksort($_SESSION['sys_breadcumb']); 
	$html = "<ul id='sys_bread_cumb'>";
	$size = count($_SESSION['sys_breadcumb']);
	$i = 0;
	foreach($_SESSION['sys_breadcumb'] as $item){
		if(!empty($item)){
			$i++ ;
			if( $i== $size){	
				$html .= "<li class='active'><a href='".$item[1]."'>".$item[0]."</a></li>";
			}else{
				$html .= "<li><a href='".$item[1]."'>".$item[0]."</a></li>";
			}	
		}	
	}
	$html .= "</ul>";
	//breadcumb_reset();
	
	return "<script type='text/javascript'>
				$(document).ready(function(){
					$('#breadcumb').html(\"$html\");
				});
			</script>";
}
 
function multi_checkbox($name_array = "multi_checkbox[]", $value_key_array= array(), $value_checked=array(), $divStyle='', $inputStyle ='', $isCheckAll = false){
	$height = count($value_key_array) >5 ? '100px':'auto';
	$html = ($divStyle !='') ? "<div $divStyle>":"<div style='width:150px;height:$height;float:left;overflow:auto;border:1px solid #222222;'>";
	if($isCheckAll){
		$nameCheckAll = str_replace('[]','_checkall',$name_array);
		$class = $inputStyle != '' ? "class=\"$inputStyle\"":'class="custom"';
		$html .=    "<div class='checkbox-wrapper' style='border-bottom:1px solid;'>
					<input type='checkbox' id='$nameCheckAll' $class />
					<label>".language_translate('html_form_helper_checkall')."</label></div>";
	}
	foreach($value_key_array as $key => $value){
		$checked = in_array($key, $value_checked) ? "checked='checked'":'';
		//$html .= ($inputStyle !='') ? "<div $inputStyle>":"<div style='width:100%;float:left;clear:both;color:#828282;font-size:12px;'>";
		$html .= "<div class=\"checkbox-wrapper\">";
		$class = $inputStyle != '' ? "class=\"$inputStyle\"":'class="custom"';
		$html .= "<input type='checkbox' name='$name_array' value='$key' $checked $class /><label>$value</label>";
		$html .= "</div>";
	}
	$html .= "</div>";
	if($isCheckAll){
		$name = str_replace('[]','',$name_array);
		$html .= "<script type='text/javascript'>
				$(document).ready(function() {
					$('#$nameCheckAll').click(function(){
						if($('#$nameCheckAll').attr('checked')){
							$('input[name=$name\\\[\\\]]').attr('checked',false);
						}else{
							$('input[name=$name\\\[\\\]]').attr('checked',true);
						}
					});
				});
				</script>";
	}
	return $html;
} 

function form_multi_checkbox($name_array = "multi_checkbox", $value_key_array= array(), $value_checked=array(), $divStyle='', $inputStyle ='', $isCheckAll = false){
	$height = count($value_key_array) >5 ? '100px':'auto';
	$html = ($divStyle !='') ? "<div $divStyle>":"<div style='width:150px;height:$height;float:left;overflow:auto;border:1px solid #222222;'>";
	if($isCheckAll){
		$nameCheckAll = str_replace('[]','_checkall',$name_array);
		$class = $inputStyle != '' ? "class=\"$inputStyle\"":'class="custom"';
		$html .=    "<div class='checkbox-wrapper' style='border-bottom:1px solid;'>
					<input type='checkbox' id='$nameCheckAll' $class />
					<label>".language_translate('html_form_helper_checkall')."</label></div>";
	}
	foreach($value_key_array as $key => $value){
		$checked = in_array($key, $value_checked) ? "checked='checked'":'';
		//$html .= ($inputStyle !='') ? "<div $inputStyle>":"<div style='width:100%;float:left;clear:both;color:#828282;font-size:12px;'>";
		$html .= "<div class=\"checkbox-wrapper\">";
		$class = $inputStyle != '' ? "class=\"$inputStyle\"":'class="custom"';
		$html .= "<input type='checkbox' name='$name_array"."[".$key."]' value='$key' $checked $class /><label>$value</label>";
		$html .= "</div>";
	}
	$html .= "</div>";
	if($isCheckAll){
		$name = str_replace('[]','',$name_array);
		$html .= "<script type='text/javascript'>
				$(document).ready(function() {
					$('#$nameCheckAll').click(function(){
						if($('#$nameCheckAll').attr('checked')){
							$('input[name=$name\\\[\\\]]').attr('checked',false);
						}else{
							$('input[name=$name\\\[\\\]]').attr('checked',true);
						}
					});
				});
				</script>";
	}
	return $html;
}

function contractDiscountSelectionForm($id='df1',$class='',$js='',$period=1,$customeStyle=true){
	$class2 = $customeStyle ? 'custom':'normal';
	$class2 .= " $class";
	$html = "<select class='$class2' id='$id' $js>";
	foreach(contractDiscount_ioc() as $key=> $item){
		$selected = ($key == $period) ? 'selected="selected"' :'';
		$discount = contractDiscount_ioc($key);
		$html .= "<option value='$key' $selected>$key ".language_translate('contract_discount_month').' ('.$discount.language_translate('contract_percent_discount').') '."</option>";
	}
	$html .= "</select>";
	return $html;
}

function buildCustomProfileLink($escort_id,$is_search = false){
	$ci = &get_instance();
	$current_dbprefix = $ci->db->dbprefix;
	$ci->db->set_dbprefix('default_');
	
	$ESCORTOBJ =  $ci->db->where('id',$escort_id)->get(TBL_ESCORT)->result();
	$listingArr = array_filter(explode(',',$ESCORTOBJ[0]->places_list_ids_contract));
	
	if($listingArr){
		$firstObj = $ci->db->where('id',$listingArr[0])->get(TBL_PLACE_LIST)->result();
	}
	
	if($is_search){
		if(isset($_SESSION['location_ids'])){
			$ex = explode('|', $_SESSION['location_ids']);
			$country_id= $ex[0];
			$state_id= $ex[1];
			$province_id= $ex[2];
			$city_id= $ex[3];
		}else{
			$country_id=$firstObj[0]->country_id;
			$state_id=0;
			$province_id=0;
			$city_id=0;
		}
	}else{
		$country_id=($firstObj)?$firstObj[0]->country_id:0;
		$state_id=0;
		$province_id=0;
		$city_id=0;
	}
	
	if(!$country_id){
		$country_id = $ESCORTOBJ[0]->country_id;
	}
	
	/**
	$DEFAULT_COUNTRY = getCountryInfoFromUserGeoLocation();
	$pre = array_keys(getCountriesOfEscortsUsed());
	$preCountry = ($DEFAULT_COUNTRY) ? $DEFAULT_COUNTRY->getId() : $pre[0];
	//$cityData = getCityInfoFromUserGeoLocation();
	
	$df_loc_country_id = $preCountry;
	//$df_loc_state_id = $cityData['state_id'];
	//$df_loc_city_id = $cityData['city_id'];
	**/
	
		
	if(!isset($_SESSION['location_ids'])){
		if( $tmp = searchListingPlaceInfoDetails($country_id,$state_id,$province_id,$city_id,$listingArr) ){
			list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
		}else{
			$c_id = $ESCORTOBJ[0]->country_id;
			$s_id = $ESCORTOBJ[0]->state_id ;
			$p_id = $ESCORTOBJ[0]->province_id;
			$ci_id = $ESCORTOBJ[0]->city_id;
		}
	}else{
		$c_id = $country_id ;
		$s_id = $state_id ;
		$p_id = $province_id ;
		$ci_id = $city_id ;
		//if($s_id == -1){
			if( $tmp = searchListingPlaceInfoDetails($c_id,$s_id,$p_id,$ci_id,$listingArr) ){
				list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
			}
		//}
	}
	
	$COUNTRY = $ci->countries_entity->init('id',$c_id);
	
	/**
	$s_id = ($s_id>0)?$s_id:-2;
	$p_id = ($p_id>0)?$p_id:-2;
	$ci_id = ($ci_id>0)?$ci_id:-2;
	
	$country_arr = $state_arr = $province_arr = $city_arr = array();
	
	$country_arr = getCountryData_ioc( $c_id );
	if($s_id > 0){
		$state_arr = getStateData_ioc( $c_id, $s_id );
	}
	
	if($s_id > 0 AND $p_id > 0){
		$province_arr = getProvinceData_ioc( $c_id, $s_id, $p_id );
	}
	
	if($s_id > 0 AND $p_id > 0 AND $ci_id > 0){
		$city_arr = getCityData_ioc( $c_id, $s_id, $p_id, $ci_id );
	}
	**/
	//$countrySegment = (!empty($country_arr)) ? '/'.slugify(getValueOfArray($country_arr)) : '';
	$countrySegment = ($COUNTRY) ? '/'.slugify($COUNTRY->getCountryCode()) : '';
	$stateSegment = (!empty($state_name)) ? '/'.slugify($state_name) : '';
	$provinceSegment = (!empty($province_name)) ? '/'.slugify($province_name) : '';
	$citySegment = (!empty($city_name)) ? '/'.slugify($city_name) : '';
	$escort_name = ($ESCORTOBJ[0]->escort_name) ? '/'.$escort_id.'/'.slugify($ESCORTOBJ[0]->escort_name) : '/'.$escort_id.'/~';
	//$url = site_url("home/profile/$escort_id").$countrySegment.$stateSegment.$provinceSegment.$citySegment.$escort_name;
	$url = site_url_lang("escort").$countrySegment.$stateSegment.$provinceSegment.$citySegment.$escort_name;
	
	$ci->db->set_dbprefix($current_dbprefix);
	return $url;
}

function buildBreadCrumbEscortProfile($escort_id){
	$ci = &get_instance();
	$current_dbprefix = $ci->db->dbprefix;
	$ci->db->set_dbprefix('default_');
	
	$ESCORTOBJ =  $ci->db->where('id',$escort_id)->get(TBL_ESCORT)->result();
	$listingArr = array_filter(explode(',',$ESCORTOBJ[0]->places_list_ids_contract));
	if($listingArr){
		$firstObj = $ci->db->where('id',$listingArr[0])->get(TBL_PLACE_LIST)->result();
	}
	
	if(isset($_SESSION['location_ids'])){
		$ex = explode('|', $_SESSION['location_ids']);
		$country_id= $ex[0];
		$state_id= $ex[1];
		$province_id= $ex[2];
		$city_id= $ex[3];
	}else{
		$country_id=($firstObj)?$firstObj[0]->country_id:0;
		$state_id=0;
		$province_id=0;
		$city_id=0;
	} 
	
	if(!$country_id){
		$country_id = $ESCORTOBJ[0]->country_id;
	}
	
	/**
	$DEFAULT_COUNTRY = getCountryInfoFromUserGeoLocation();
	$pre = array_keys(getCountriesOfEscortsUsed());
	$preCountry = ($DEFAULT_COUNTRY) ? $DEFAULT_COUNTRY->getId() : $pre[0];
	$cityData = getCityInfoFromUserGeoLocation();
	
	$df_loc_country_id = $preCountry;
	$df_loc_state_id = $cityData['state_id'];
	$df_loc_city_id = $cityData['city_id'];
	**/
	
		
	if(!isset($_SESSION['location_ids'])){
		if( $tmp = searchListingPlaceInfoDetails($country_id,$state_id,$province_id,$city_id,$listingArr) ){
			list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
		}else{
			$c_id = $ESCORTOBJ[0]->country_id;
			$s_id = $ESCORTOBJ[0]->state_id ;
			$p_id = $ESCORTOBJ[0]->province_id;
			$ci_id = $ESCORTOBJ[0]->city_id;
		}
	}else{
		$c_id = $country_id ;
		$s_id = $state_id ;
		$p_id = $province_id ;
		$ci_id = $city_id ;
		//if($s_id == -1){
			if( $tmp = searchListingPlaceInfoDetails($c_id,$s_id,$p_id,$ci_id,$listingArr) ){
				list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
			}
		//}
	}
	/**
	$s_id = ($s_id>0)?$s_id:-2;
	$p_id = ($p_id>0)?$p_id:-2;
	$ci_id = ($ci_id>0)?$ci_id:-2;
	
	$country_arr = $state_arr = $province_arr = $city_arr = array();
	
	$country_arr = getCountryData_ioc( $c_id );
	if($s_id > 0){
		$state_arr = getStateData_ioc( $c_id, $s_id );
	}
	
	if($s_id > 0 AND $p_id > 0){
		$province_arr = getProvinceData_ioc( $c_id, $s_id, $p_id );
	}
	
	if($s_id > 0 AND $p_id > 0 AND $ci_id > 0){
		$city_arr = getCityData_ioc( $c_id, $s_id, $p_id, $ci_id );
	}
	**/
	breadcumb(2, array( $country_name,site_url_lang("home/search?country_id=".$c_id."&state_id=-1")));
	
	if($state_name){	
		breadcumb(3, array( $state_name,
					site_url_lang("home/search?country_id=".$c_id.'&state_id='.$s_id)));
	}					
	if($province_name){				
		breadcumb(4, array($province_name,
						site_url_lang("home/search?country_id=".$c_id.'&state_id='.$s_id.'&province_id='.$p_id)
				)
			);
	}		
	if($city_name){
		breadcumb(5, array($city_name,
						site_url_lang("home/search?country_id=".$c_id.'&state_id='.$s_id.'&province_id='.$p_id.'&city_id='.$ci_id)
				)
			); 	
	}
	
	breadcumb(6, array($ESCORTOBJ[0]->escort_name,buildCustomProfileLink($escort_id)));	
	$ci->db->set_dbprefix($current_dbprefix); 
}

function getLocationZoneOfEscort($escort_id){
	$ci = &get_instance();
	$current_dbprefix = $ci->db->dbprefix;
	$ci->db->set_dbprefix('default_');
	
	$ESCORTOBJ =  $ci->db->where('id',$escort_id)->get(TBL_ESCORT)->result();
	$listingArr = array_filter(explode(',',$ESCORTOBJ[0]->places_list_ids_contract));
	if($listingArr){
		$firstObj = $ci->db->where('id',$listingArr[0])->get(TBL_PLACE_LIST)->result();
	}
	
	if(isset($_SESSION['location_ids'])){
		$ex = explode('|', $_SESSION['location_ids']);
		$country_id= $ex[0];
		$state_id= intval($ex[1]);
		$province_id= intval($ex[2]);
		$city_id= intval($ex[3]);
	}else{
		$country_id=($firstObj)?$firstObj[0]->country_id:0;
		$state_id=0;
		$province_id=0;
		$city_id=0;
	} 
	
	if(!$country_id){
		$country_id = $ESCORTOBJ[0]->country_id;
	}
	
	/*
	$DEFAULT_COUNTRY = getCountryInfoFromUserGeoLocation();
	$pre = array_keys(getCountriesOfEscortsUsed());
	$preCountry = ($DEFAULT_COUNTRY) ? $DEFAULT_COUNTRY->getId() : $pre[0];
	//$cityData = getCityInfoFromUserGeoLocation();
	
	$df_loc_country_id = $preCountry;
	//$df_loc_state_id = $cityData['state_id'];
	//$df_loc_city_id = $cityData['city_id'];
	$listingArr = array_filter(explode(',',$ESCORTOBJ[0]->places_list_ids_contract));
	*/
		
	if(!isset($_SESSION['location_ids'])){
		if( $tmp = searchListingPlaceInfoDetails($country_id,$state_id,$province_id,$city_id,$listingArr) ){
			list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
		}else{
			$c_id = $ESCORTOBJ[0]->country_id;
			$s_id = $ESCORTOBJ[0]->state_id ;
			$p_id = $ESCORTOBJ[0]->province_id;
			$ci_id = $ESCORTOBJ[0]->city_id;
		}
	}else{
		$c_id = $country_id ;
		$s_id = $state_id ;
		$p_id = $province_id ;
		$ci_id = $city_id ;
		//if($s_id == -1){
			if( $tmp = searchListingPlaceInfoDetails($c_id,$s_id,$p_id,$ci_id,$listingArr) ){
				list($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name) = $tmp;
			}
		//}
	}
	
	/**
	$s_id = ($s_id>0)?$s_id:-2;
	$p_id = ($p_id>0)?$p_id:-2;
	$ci_id = ($ci_id>0)?$ci_id:-2;
	
	$ci->db->set_dbprefix($current_dbprefix);
	
	$country_arr = $state_arr = $province_arr = $city_arr = array();
	
	$country_arr = getCountryData_ioc( $c_id );
	if($s_id > 0){
		$state_arr = getStateData_ioc( $c_id, $s_id );
	}
	
	if($s_id > 0 AND $p_id > 0){
		$province_arr = getProvinceData_ioc( $c_id, $s_id, $p_id );
	}
	
	if($s_id > 0 AND $p_id > 0 AND $ci_id > 0){
		$city_arr = getCityData_ioc( $c_id, $s_id, $p_id, $ci_id );
	}
	*/
	 
	if($city_name){
		return $city_name;
	}
	if($province_name){
		return $province_name;
	}
	if($state_name){
		return $state_name;
	}
	if($country_name){
		return $country_name;
	}
	
}

function searchListingPlaceInfoDetails($country_id,$state_id,$province_id,$city_id,$listingPlaceEscortArray){
	$ci = &get_instance();
	$current_dbprefix = $ci->db->dbprefix;
	$ci->db->set_dbprefix('default_');
	
	if($state_id !=0 OR $province_id != 0 OR $city_id != 0){
		$pri = $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST.
									" WHERE country_id=$country_id AND state_id=$state_id 
										AND province_id=$province_id AND city_id=$city_id"
								)->result();
		$id=0;
		if($pri){
			$id= $pri[0]->id;
		}else{
			$pri1 =  $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST.
									" WHERE country_id=$country_id AND state_id=$state_id 
										AND province_id=$province_id "
								)->result();
			if($pri1){
				$id= $pri1[0]->id;
			}else{
				$pri2 =  $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST.
									" WHERE country_id=$country_id AND state_id=$state_id 
										"
								)->result();
				if($pri2){
					$id= $pri2[0]->id;
				}else{
					$pri3 =  $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST.
									" WHERE country_id=$country_id 
										"
								)->result();
					if($pri3){
						$id= $pri3[0]->id;
					}			
				}				
			}
		}
	
	}else{
		$pri = $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST.
									" WHERE country_id=$country_id "
								)->result();
								
		$pri1= array();
		$pri2 = array();
		$pri3 = array();
		$pri4 = array();
			
		foreach($pri as $item){
			if(in_array($item->id,$listingPlaceEscortArray)){
				
				if($item->city_id != 0 AND $item->province_id !=0 AND $item->state_id != 0 AND $item->country_id != 0 ){
					$pri1[] = $item->id;
					break;
				}
				if($item->city_id == 0 AND $item->province_id != 0 AND $item->state_id !=0 AND $item->country_id != 0  ){
					$pri2[] = $item->id;
					break;
				}
				if($item->city_id !== 0 AND $item->province_id == 0 AND $item->state_id !=0 AND $item->country_id != 0 ){
					$pri3[] = $item->id;
					break;
				}
				if($item->city_id == 0 AND $item->province_id == 0 AND $item->state_id ==0 AND $item->country_id != 0 ){
					$pri4[] = $item->id;
					break;
				}
			
			}
		}	
		
		$id = 0;
		if(!empty($pri0)){
			$id = ($id == 0) ? $pri0[0] :$id;
		}
		if(!empty($pri1)){
			$id = ($id == 0) ? $pri1[0] :$id;
		}
		if(!empty($pri2)){
			$id = ($id == 0) ? $pri2[0] :$id;
		}
		if(!empty($pri3)){
			$id = ($id == 0) ? $pri3[0] :$id;
		}
		if(!empty($pri4)){
			$id = ($id == 0) ? $pri4[0] :$id;
		}
		 
	}
	$ci->db->set_dbprefix($current_dbprefix);
	 
	if(!$id){
		return false;
	}else{
		$oj = $ci->db->where('id',$id)->get(TBL_PLACE_LIST)->result();
		$c_id = $oj[0]->country_id;
		$s_id = $oj[0]->state_id ;
		$p_id = $oj[0]->province_id;
		$ci_id = $oj[0]->city_id;
		
		$country_name = $oj[0]->country_name;
		$state_name = $oj[0]->state_name;
		$province_name = $oj[0]->province_name;
		$city_name = $oj[0]->city_name;
		  
		$s_id = ($s_id == 0)? -2:$s_id;
		return array($c_id,$s_id,$p_id,$ci_id,$country_name,$state_name,$province_name,$city_name);
	}
}



